//
//  ScrollViewSlider.m
//  GuoBuGaForIphone
//
//  Created by zhangwo on 16/10/26.
//  Copyright © 2016年 GuoBuGa. All rights reserved.
//

#import "ScrollViewSlider.h"

@implementation ScrollViewSlider

@synthesize scrollView  = _scrollView;
@synthesize pageControl = _pageControl;
@synthesize dataList = _dataList;

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor grayColor];
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        
        [self addSubview:_scrollView];
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((frame.size.width-100)/2, frame.size.height-30, self.frame.size.width, 20)];
        [self addSubview:_pageControl];
        
        NSString *spath = [[NSBundle mainBundle] pathForResource:@"SliderImage" ofType:@"plist"];
        NSArray *sliderData = [NSArray arrayWithContentsOfFile:spath];
        _dataList = sliderData;
        
    }
    return self;
}
- (void)setDataList:(NSArray *)dataList{
    _dataList = dataList;
    
    NSInteger i =0;
    CGFloat w = self.frame.size.width;
    CGFloat h = self.frame.size.height;
    for (NSDictionary *item in _dataList) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(w*i, 0, w, h)];
        imageView.image = [UIImage imageNamed:[item objectForKey:@"image"]];
        imageView.contentMode = UIViewContentModeScaleToFill;
        [_scrollView addSubview:imageView];
        
        i++;
    }
    [_scrollView setContentSize:CGSizeMake(w*i, 0)];
    [_pageControl setNumberOfPages:i];
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(autoPlay) userInfo:nil repeats:YES];
    
}

- (void)autoPlay{
    NSInteger index = _pageControl.currentPage;
    index++;
    [self scrollTo:index];
}

- (void)scrollTo:(NSInteger)index{
    BOOL animated = YES;
    if (index >= _pageControl.numberOfPages) {
        index = 0;
        animated = NO;
    }else {
        animated = YES;
    }
    _pageControl.currentPage = index;
    [_scrollView setContentOffset:CGPointMake(self.frame.size.width*index, 0) animated:animated];
}

- (void)imageViewTap:(UITapGestureRecognizer *)tap{
    if ([_delegate respondsToSelector:@selector(scrollViewSlider:didSelectedItemWithData:)]) {
        [_delegate scrollViewSlider:self didSelectedItemWithData:[_dataList objectAtIndex:tap.view.tag]];
    }
}

- (void)pageControlClick:(UIPageControl *)sender{
    [self scrollTo:sender.currentPage];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *data = [_dataList objectAtIndex:[indexPath row]];
    if (_delegate && [_delegate respondsToSelector:@selector(scrollViewSlider:didSelectedItemWithData:)]) {
        [_delegate scrollViewSlider:self didSelectedItemWithData:data];
    }
}

#pragma mark - scrollView delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = self.scrollView.contentOffset.x/self.frame.size.width;
    [_pageControl setCurrentPage:index];
}

@end
